package com.sskj.zixun.ui.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.sskj.zixun.R;
import com.sskj.zixun.R2;
import com.sskj.zixun.bean.PlatFormInfoBean;
import com.sskj.zixun.http.HttpConfig;
import com.sskj.zixun.presenter.ZixunReallyFragmentPresenter;
import com.sskj.zixun.ui.activity.ZixunDetailActivity;
import com.sskj.zixun.utils.ZzUtil;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.common.base.App;
import com.sskj.common.box.imageloader.GlideImageLoader;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 资讯模块
 */
@Route(path = RConfig.ZIXUN_FRAGMENT_MAIN)//行情咨询主页
public class ZiXunReallyFragment extends BaseFragment<ZixunReallyFragmentPresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.ivBack)
    ImageView ivBack;
    @BindView(R2.id.zixun_recyclerView)
    RecyclerView zixunRecyclerView;
    @BindView(R2.id.zixun_refresh)
    SmartRefreshLayout ziXunRefresh;
    private BaseAdapter<PlatFormInfoBean.InfoBean> adapter;
    private String[] valueUrl;
    private Banner banner;
    private int page = 1;  // 默认加载页
    private int pageSize = 4;  // 默认一页多少条数据
    private ArrayList<String> images;
    private boolean isBannerShow = false;

    @Override
    protected int getLayoutId() {
        return R.layout.zixun_fragment_really;
    }

    @Override
    protected ZixunReallyFragmentPresenter getPresenter() {
        return new ZixunReallyFragmentPresenter();
    }

    @Override
    public void initView() {
        super.initView();
        tvTitle.setText(App.INSTANCE.getString(R.string.zixunZiXunReallyFragment0));
        ivBack.setVisibility(View.GONE);
        //设置图片集合
        images = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        zixunRecyclerView.setLayoutManager(linearLayoutManager);
        adapter = new BaseAdapter<PlatFormInfoBean.InfoBean>(R.layout.zixun_item_information, new ArrayList<>(), zixunRecyclerView) {
            @Override
            public void bind(ViewHolder holder, PlatFormInfoBean.InfoBean item) {
                holder.setText(R.id.tv_name, item.getTitle());
                holder.setText(R.id.tv_source, ZzUtil.changeDetails(item.getDetails()));
                ImageUtil.setImage(HttpConfig.BASE_URL + item.getImgCover(), (ImageView) holder.getView(R.id.iv_information));
//                holder.setText(R.id.notice_time, item.getCreateTime().get(0) + "  " + item.getCreateTime().get(1));
                ClickUtil.click(holder.getView(R.id.zixun_item), () -> ZixunDetailActivity.start(getActivity(), item.getId()));
            }
        };
        View inflate = View.inflate(getActivity(), R.layout.zixun_recy_header, null);
        banner = inflate.findViewById(R.id.bannerZixun);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //
        banner.setOnBannerListener(position -> {
            if (valueUrl != null && valueUrl.length > position && valueUrl[position] != null && !TextUtils.isEmpty(valueUrl[position])) {
//                BannerWebActivity.start(getActivity(), valueUrl[position]);

            }
        });
        adapter.addHeaderView(inflate);
        adapter.setEmptyView();
        ziXunRefresh.setOnRefreshListener(refreshLayout1 -> {  // 下拉刷新
            ziXunRefresh.finishRefresh();
            page = 1;
            mPresenter.getDataList(page, pageSize);
            mPresenter.getBannerUrl();
        });
        ziXunRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {  // 上拉加载
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                ziXunRefresh.finishLoadMore();
                page++;
                mPresenter.getDataList(page, pageSize);
            }
        });
        ziXunRefresh.setRefreshHeader(new MaterialHeader(getActivity()));
    }

    @Override
    public void initData() {
        mPresenter.getBannerUrl();
        mPresenter.getDataList(page, pageSize);
    }

    public void setData(List<PlatFormInfoBean.InfoBean> data) {
        if (page == 1) {
            adapter.setNewData(data);
        } else {
            adapter.addData(data);
        }
        if (zixunRecyclerView == null)
            return;
        zixunRecyclerView.setAdapter(adapter);
    }

    /**
     * 接口回调，显示Banner图
     */
    public void showBanner(com.sskj.zixun.bean.Banner bannerIamge) {
        images.clear();
        valueUrl = null;
        isBannerShow = true;
        String value = bannerIamge.getValue();  // 获取Banner图地址
        String[] valueImage = value.split(",");

        String urlValue = bannerIamge.getUrlValue();  // Banner图点击的URL链接跳转
        if (!TextUtils.isEmpty(urlValue)) {
            valueUrl = urlValue.split(",");
        }
        for (int i = 0; i < valueImage.length; i++) {
            images.add(HttpConfig.BASE_URL + valueImage[i]);
        }
        if (banner == null)
            return;
        banner.setImages(images);
        //banner设置方法全部调用完毕时最后调用
        banner.start();

    }
}
